﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

using QuDao;
using FineUICore;
using Kalman;
using Kalman.Extensions;
using Microsoft.AspNetCore.Http;
using AliyunOss;

namespace AdminUI.Pages.BaseData
{
    public partial class AlbumAddBatchModel : BaseModel
    {
        readonly AliyunOssClient oss;
        public AlbumAddBatchModel(AliyunOssClient ossClient)
        {
            oss = ossClient;
        }

        public void OnGet(int rid)
        {
            GoodsID = rid;
        }

        /// <summary>
        /// 
        /// </summary>
        public int GoodsID { get; set; }

        public IActionResult OnPostBtnSubmit_Click(int goodsID, string memo, List<IFormFile> albumFileIDs)
        {
            try
            {
                var list = new List<AlbumAddDto>();
                var imgList = new List<FileDto>();
                if (albumFileIDs.Count > 0)
                {
                    imgList = UploadImageBatch(oss, albumFileIDs);

                    foreach (var img in imgList)
                    {
                        list.Add(new AlbumAddDto
                        {
                            ImageUrl = img.OssUrl,
                            Width = img.Width,
                            Height = img.Height,
                            FileSize = (int)img.FileSize,
                            GoodsID = goodsID,
                            Memo = memo,
                        });
                    }
                }
                else
                {
                    Alert.Show("请上传至少一张图片", "提示信息", MessageBoxIcon.Warning);
                    return UIHelper.Result();
                }

                AlbumBO.Instance.BatchAdd(list);
                //LogAction(ActionType.新增, $"批量新增相册图片：{relationID}-{albumType}-{list.Count}张图");


                FineUICore.PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
                //ActiveWindow.HidePostBack();
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
                if (!(ex is AppException && ex.InnerException == null))
                {
                    msg = ex.ToString();
                }

                Alert.Show(msg, "错误信息", MessageBoxIcon.Error);
            }

            return UIHelper.Result();
        }

    }
}